package krt.wid.gslzs.util;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Hashtable;
import java.util.Map;

import krt.wid.gslzs.bean.UserInfo;
import krt.wid.util.ParseJsonUtil;

/**
 * @author Marcus
 * @package krt.wid.gslzs.util
 * @description
 * @time 2018/5/12
 */

public class SpUtil {
    private static final String USER = "user";

    private static final String PUSH = "push";

    private static final String CITY = "city";

    private static final String CART = "cart";

    private static final String SEARCH = "search";

    private static final String BOTTOM = "bottom";

    private Context mContext;

    public SpUtil(Context context) {
        this.mContext = context;
    }

    /**
     * 存储登陆信息
     */
    public void setUserInfo(String jsonStr) {
        SharedPreferences.Editor edit = mContext.getSharedPreferences(USER, Context.MODE_PRIVATE).edit();
        edit.putString("user", jsonStr);
        edit.apply();
    }

    /**
     * 获取登陆信息
     */
    public UserInfo getUserInfo() {
        SharedPreferences sp = mContext.getSharedPreferences(USER, Context.MODE_PRIVATE);
        return ParseJsonUtil.getBean(sp.getString("user", ""), UserInfo.class);
    }

    public String getAccessToken() {
        SharedPreferences sp = mContext.getSharedPreferences(USER, Context.MODE_PRIVATE);
        return sp.getString("accesstoken", "");
    }

    public void setAccessToken(String token) {
        SharedPreferences.Editor edit = mContext.getSharedPreferences(USER, Context.MODE_PRIVATE).edit();
        edit.putString("accesstoken", token);
        edit.apply();
    }

    public Map<String, String> getUseNamePsd() {
        SharedPreferences sp = mContext.getSharedPreferences(USER, Context.MODE_PRIVATE);
        Map<String, String> map = new Hashtable<>();
        map.put("name", sp.getString("username", ""));
        map.put("psd", sp.getString("psd", ""));
        return map;
    }

    public void setUseNamePsd(String name, String psd) {
        SharedPreferences.Editor edit = mContext.getSharedPreferences(USER, Context.MODE_PRIVATE).edit();
        edit.putString("username", name);
        edit.putString("psd", psd);
        edit.apply();
    }


}